package com.example.library_base.base

import android.content.Context
import android.os.Bundle
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import androidx.annotation.LayoutRes
import androidx.appcompat.app.ActionBar
import androidx.appcompat.widget.Toolbar
import androidx.core.util.forEach
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.example.library_base.base.qualifiers.ActivityScoped
import com.example.library_base.util.KeyBoardUtil
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity
import javax.inject.Inject

abstract class BaseActivity(@LayoutRes val layoutId: Int): RxAppCompatActivity(layoutId) {
    private var mBinding: ViewDataBinding? = null
    private lateinit var variables: SparseArray<Any>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //初始化DataBinding与ViewModel的绑定
        initViewModelBinding()
        //初始化页面
        initView()
        //初始化title
        initTitleBar()
        //进行网络数据的请求
        requestData()
    }

    private fun initViewModelBinding(){
        mBinding = DataBindingUtil.setContentView(this, layoutId)

        variables = getVariableSparseArray()

        variables.forEach { key, value ->
            mBinding?.setVariable(key, value)
        }

        //支持LiveDate绑定XML，数据改变，UI自动更新
        mBinding?.lifecycleOwner = this
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN){
            if (KeyBoardUtil.isKeyBoardHide(currentFocus, ev)){
                val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(currentFocus?.windowToken, 0)
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding?.unbind()
    }

    //刷新布局
    protected fun refreshLayout(){
        variables.forEach { key, value ->
            mBinding?.setVariable(key, value)
            mBinding?.executePendingBindings()
        }
    }

    //绑定布局与Model
    protected abstract fun getVariableSparseArray(): SparseArray<Any>

    //初始化title
    open fun initTitleBar(){
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setHomeButtonEnabled(true)
    }

    //自定义title
    open fun initTitleBar(@LayoutRes layoutId: Int){
        supportActionBar?.setDisplayShowCustomEnabled(true)
        val toolbarView = LayoutInflater.from(this).inflate(layoutId, null)
        val params = ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT)
        supportActionBar?.setCustomView(toolbarView, params)
        //去除系统actionBar的留白
        (supportActionBar?.customView?.parent as Toolbar).setContentInsetsAbsolute(0, 0)
    }

    //初始化页面数据
    protected abstract fun initView()

    //进行网络请求
    protected abstract fun requestData()

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return super.onSupportNavigateUp()
    }
}